package edu.gdit.health.controller;


import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import edu.gdit.health.base.result.PageTableRequest;
import edu.gdit.health.base.result.Results;
import edu.gdit.health.model.SysDict;
import edu.gdit.health.model.SysReport;
import edu.gdit.health.service.impl.SysReportServiceImpl;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.model.IModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller       //可以对外访问，并检查访问者是否有登陆权限和操作权限
@RequestMapping("/report")//定义该控制器的访问形式
public class ReportController {
    private static final Log log = LogFactory.getLog(ApiController.class);
    //自动注入service层的对象sysReportServiceImpl,该对象由Spring容器创建并管理
    @Autowired
    SysReportServiceImpl sysReportServiceImpl;

    //以/report/add的Get形式访问，则进入/templates/report/add.html页面
    @RequestMapping("/add")
    public String add(Model model){
        //定义一个获取数据字典的对象dict
        Map<String, List<SysDict>> dict=sysReportServiceImpl.getDictSet();
        //将dict返回到前端页面
        model.addAttribute("dict",dict);
        return "/report/add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Results addPost(@RequestBody SysReport sysReport) {
        log.info("用户调用了add方法");
        //调用业务层对象sysReportServiceImpl的addReport方法，增加一条记录
        return sysReportServiceImpl.addSysReport(sysReport);
    }

    @RequestMapping("/dictById")
    public String update(Model model,SysReport sysReport){
        //定义一个获取数据字典的对象dict
        Map<String, List<SysDict>> dict=sysReportServiceImpl.getDictSet();
        //将dict返回到前端页面
        model.addAttribute("dict",dict);
        SysReport sysReportById = sysReportServiceImpl.dictById(sysReport);
        model.addAttribute("sysReportbyId",sysReportById);
        return "/report/update";
    }
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public int updata(@RequestBody SysReport sysReport){
        log.info("用户调用了update方法");
        System.out.println("后端接收更新对象："+sysReport);
        return sysReportServiceImpl.updateReport(sysReport);
    }

    @RequestMapping(value = "/getById")
    @ResponseBody
    public Results getById(int id){
        System.out.println("id查询 controller层："+id);
        return sysReportServiceImpl.getById(id);
    }
    @RequestMapping(value = "/list")
    @ResponseBody
    public Results list(PageTableRequest pageTableRequest) {
        return sysReportServiceImpl.getAll(pageTableRequest);
    }
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Results delete(int id){
        log.info("用户调用了delete方法");
        return sysReportServiceImpl.deleteReport(id);
    }

    //springMvc框架自动获取同名参数值
//    @RequestMapping(value = "/list")
//    @ResponseBody
//    public Results list(int page,int limit){
//        int offset=(page-1)*limit;
//        return sysReportServiceImpl.getAllReports(offset,limit);
//    }
}


